package com.emasdemo.receiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.alibaba.sdk.android.push.MessageReceiver;
import com.alibaba.sdk.android.push.notification.CPushMessage;
import com.emasdemo.R;

import java.util.Map;

import static android.content.Context.NOTIFICATION_SERVICE;

public class MyMessageReceiver extends MessageReceiver {
    // 消息接收部分的LOG_TAG
    public static final String REC_TAG = "MPS:MyMessageReceiver";
    public NotificationManager manager;

    @Override
    public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
        // TODO 处理推送通知
        Log.e(REC_TAG, "Receive notification, title: " + title + ", summary: " + summary + ", extraMap: " + extraMap);
    }

    @Override
    public void onMessage(Context context, CPushMessage cPushMessage) {
        Log.e(REC_TAG, "onMessage, messageId: " + cPushMessage.getMessageId() + ", title: "
                + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent() + ", messageId"
                + cPushMessage.getMessageId() + ", traceInfo:" + cPushMessage.getTraceInfo());

        //推送消息类型时还需要创建通知时使用
//        myNotification(context, cPushMessage);
    }

    @Override
    public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
        Log.e(REC_TAG, "onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
    }

    @Override
    protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
        Log.e(REC_TAG, "onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
    }

    @Override
    protected void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl) {
        Log.e(REC_TAG, "onNotificationReceivedInApp, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap + ", openType:" + openType + ", openActivity:" + openActivity + ", openUrl:" + openUrl);
    }

    @Override
    protected void onNotificationRemoved(Context context, String messageId) {
        Log.e(REC_TAG, "onNotificationRemoved，" + "  messageId:" + messageId);
    }

    public void myNotification(Context context, CPushMessage cPushMessage) {
        if (manager == null) {
            manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "8.0up");//该8.0up,对应初始化时的NotificationChannel的id
        builder.setSmallIcon(R.drawable.logo_yuanjiao_120)//小图标
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))//大图标
                .setTicker("这是一个本地通知")//状态栏提示文字
                .setContentTitle("这是一个本地通知")//标题
                .setContentText("本地通知创建成功")//内容
                .setAutoCancel(true)//点击后自动删除
                .setDefaults(Notification.DEFAULT_ALL)
                .setColor(Color.RED)//设置颜色
                .setContentIntent(PendingIntent.getActivity(context, 0, new Intent("com.emas.main"), 0))//点击后意图
                .setWhen(System.currentTimeMillis());
        manager.notify(1, builder.build());//id不能重复，此处只是示例
    }
}
